﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace Nirvana
{
    public static class TypeExtensions
    {
        public static bool IsStruct(this Type type)
        {
            return type.IsValueType && !type.IsPrimitive && !type.IsEnum;
        }

        public static FieldInfo[] GetFieldInfosIncludingBaseClasses(this Type type, BindingFlags bindingFlags)
        {
            FieldInfo[] fields = type.GetFields(bindingFlags);
            if (type.BaseType == typeof(object))
            {
                return fields;
            }
            List<FieldInfo> list = new List<FieldInfo>(fields);
            while (type.BaseType != typeof(object))
            {
                type = type.BaseType;
                fields = type.GetFields(bindingFlags);
                foreach (FieldInfo fieldInfo in fields)
                {
                    bool flag = false;
                    foreach (FieldInfo fieldInfo2 in list)
                    {
                        if (fieldInfo2.Name == fieldInfo.Name && fieldInfo2.DeclaringType == fieldInfo.DeclaringType)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        list.Add(fieldInfo);
                    }
                }
            }
            return list.ToArray();
        }

    }
}

